<?php

namespace app\api\controller;

use app\common\controller\Api;

/**
 * 问卷调查接口
 */
class Question extends Api
{

    protected $noNeedLogin = ["*"];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    // 获取所有调查问卷
    public function index()
    {
        $classid = $this->request->request('classid');//分类ID

        if(empty($classid)){
            $this->error("数据错误");
        }

        $where = [];
        $where['classid'] = $classid;
        $where['status'] = 0;

        //查询问卷列表
        $question_list = db('question')->where($where)->order('sort', 'desc')->select();
        if(!empty($question_list)){
            foreach($question_list as $key=>$value){
                //查询用户测试数量
                $counts = db('user_testlog')->where('question_id', $value['id'])->count();
                $question_list[$key]['counts'] = $counts;
                $question_list[$key]['cover'] = getImagesUrl($value['cover']);
            }
        }

        $this->success('查询成功',$question_list);
    }

    /**
     * 调查问卷详情
     */

    public function details(){

        $id = $this->request->request('id');//分类ID

        if(empty($id)){
            $this->error("数据错误");
        }

        $data = [];

        $question_info = db('question')->where('id', $id)->find();

        $data['title'] = $question_info['title'];//标题
        $data['describe'] = $question_info['describe'];//简介

        if(empty($question_info['question'])){
            $this->error("无试题数据");
        }
        //查询试题
        $qu_list = [];
        $question = json_decode($question_info['question'],true);
        foreach($question as $key=>$value){

            if(empty($value['id'])){
                continue;
            }

            $qu_info = db('question_list')->where('id', $value['id'])->find();

            $qu_list[$key]['title'] = $qu_info['title'];
            $qu_list[$key]['settingdata'] = $qu_info['settingdata'];

        }

        $data['q_list'] = $qu_list;

        $this->success('查询成功',$data);
        
    }


    /**
     * 查询问卷结果
     */
    public function answer(){

        $id = $this->request->request('id');//分类ID
        $user_id = $this->request->request('user_id');//用户ID
        $answer = $this->request->request('answer');//答案

        if(empty($id) || empty($answer)){
           $this->error("数据错误");
        }
        $answer = htmlspecialchars_decode($answer);
        $answer = json_decode($answer,true);

        //计算个人得分
        $score = 0;
        foreach($answer as $key=>$value){
            $score += $value['score'];
        }

        //查询问卷结果信息
        $question_info = db('question')->where('id', $id)->find();
        $result = json_decode($question_info['result'],true);//分量结果

        $tag_id = 0;
        $content = "";//结果
        //判断是否有答案
        foreach($result as $k=>$v){
            if(($score >= $v['left']) && ($score <= $v['right'])){
                $tag_id = $v['tags_id'];
                $content = $v['content'];
                break;//终止循环
            }
        }
 
        //查询推荐咨询师，录播课
        if(empty($tag_id)){
            //按照分类推荐
            $teacher_list = $this->getrecteacher("classid",$question_info['classid']);//推荐老师
            $course_list = $this->getreccourse("classid",$question_info['classid']);//推荐课程
        }else{
            //按照标签
            $teacher_list = $this->getrecteacher("tags",$tag_id);//推荐老师
            $course_list = $this->getreccourse("tags",$tag_id);//推荐老师
        }

        //记录用户测试表
        $logs = ['user_id' => $user_id, 'question_id' => $id, 'answer' => json_encode($answer), 'test_result' => $content, 'status' => 0, 'createtime' => time()];
        db('user_testlog')->insert($logs);

        $data = ['content'=>$content,'teacher_list'=>$teacher_list,'course_list'=>$course_list];
        $this->success('请求成功',$data);
    }


    /**
     * 获取推荐教师
     */

    public function getrecteacher($k = '', $v = ''){
   
        $teacher_list = db('teacher')->where($k,'like','%'.$v.'%')->where('status',0)->order('sort', 'desc')->select();
  
        if(!empty($teacher_list)){
            foreach($teacher_list as $key=>$value){

                if(stristr($value[$k],",") !== false){
                    $new_array = explode(",", $value[$k]);
                }else{
                    $new_array = array($value[$k]);
                }

                $teacher_list[$key]['headimg'] = getImagesUrl($value['headimg']);
                $teacher_list[$key]['classid_title'] = model("\app\admin\model\allclass\Allclass")->getClassidInfo($value['classid']);//分类
                $teacher_list[$key]['tags_title'] = model("\app\admin\model\allclass\Tags")->get_tags($value['tags']);//标签
                $teacher_list[$key]['order_count'] = 0;//咨询数量
                $teacher_list[$key]['praise_rate'] = "0%";//好评率
                $teacher_list[$key]['start_price'] = "$0起";//咨询起价

                if(!in_array($v,$new_array)){
                    unset($teacher_list[$key]);
                }
            }
        }else{
            $teacher_list = array();
        }

        /*只取3个*/
        $t_count = count($teacher_list);
        if($t_count >= 3){
            foreach($teacher_list as $kk=>$vv){
                if($kk > 2){
                    unset($teacher_list[$kk]);
                }
            }
        }
        return $teacher_list;
    }


   /**
     * 获取推荐录播课
     */

    public function getreccourse($k = '', $v = ''){

        if($k == "classid"){
            $course_list = db('course')->where('classid',$v)->where('status',0)->order('sort', 'desc')->limit(3)->select();
        }else{
            $course_list = db('course')->where('tags','like','%#'.$v.'#%')->where('status',0)->order('sort', 'desc')->limit(3)->select();
        }

        if(!empty($course_list)){
            foreach($course_list as $key=>$value){
                $course_list[$key]['cover'] = getImagesUrl($value['cover']);
                $course_list[$key]['classid_title'] =model("\app\admin\model\allclass\Allclass")->get_class_name($value['classid']);//分类
            }
        }else{
            $course_list = array();
        }

        return $course_list;
    }






}
